<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\Models\Site;
use App\Models\Video;
use App\Models\Ad;
use App\Models\User;
use App\Models\UserVideo;
class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth')->except(['show', 'view_site', 'video']);
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $user = Auth::user();
        if ($user->role < 1) {
            // 跳转总后台
            abort(403, '您的账号已被禁止！');
        }
        // 普通用户或代理后台的跳转
        else return redirect('/notices');
    }

    public function show($site, Request $request)
    {
        $site_info = Site::with('user')->with(['ads' => function ($query) {
          $query->orderBy('id', 'asc');
        }])->find($site);
        $configs = [
            'outtime' => setting('outtime', 3),
            'sid' => config('services.tx_tongji.sid') ?: setting('sid', 500673179),
            'cid' => config('services.tx_tongji.cid') ?: setting('cid', 500673182),
            'qq' => setting('qq', ''),
            'lianxi_text' => setting('lianxi_text', '联系方式'),
            'lianxi_flash' => setting('lianxi_flash', '1')
        ];
        // 判断是否是代理的站点
        $user = $site_info->user;
        $qq = '';
        if ($user->hasRole('Daili')) {
            $qq = $user->qq;
        } /*else if($parent_id = $user->hasSuperior()) {
            $daili = User::find($parent_id);
            $qq = $daili->qq;
        }*/
        $configs['qq'] = $qq ?: setting('qq', '');
        // 视频链接的获取
        $video_id = $request->input('video', 0);  // 从请求中获取请求的视频ID
        if ($video_id < 1) {
            $video = Video::where('user_id', '>', 0)->get();
            if ($video->count() > 0) {
                $video = $video->random(1); // ->toArray();
                $video = $video[0];
            }
        } else {
            if ($video = UserVideo::find($video_id)) {
                if($video->random_id > 0) {
                    $video = UserVideo::where('random_id', $video->random_id)
                      ->where('site_id', $video->site_id)
                      ->where('user_id', $video->user_id)
                      ->get()->random();
                  }
            }
        }
        $site_info->makeHidden('user');
        return compact('site_info', 'configs', 'video');
    }
    public function video()
    {
        $videos = Video::where('user_id', '>', 0)->get();
        if($videos->count() > 0) {
          return $videos->random(1)->pluck('link')->toArray();
        }
        return '';
    }
    public function view_site(Site $site)
    {
        $ads = $site->ads;
        $user_id = $site->user_id;
        $videos = Video::where(['user_id' => $user_id])->get();
        return ['error_no' => 0, 'error_msg' => '', 'datas' => ['site' => $site, 'ads' => $ads, 'videos' => $videos]];
    }
}
